package com.yunying.rispro.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yunying.rispro.exception.BusinessException;
import com.yunying.rispro.model.*;
import com.yunying.rispro.service.IDiagnoseService;
import com.yunying.rispro.service.IEquipmentService;
import com.yunying.rispro.service.IExamineService;
import com.yunying.rispro.service.IPatientsService;
import com.yunying.rispro.util.JsonResponseBody;
import com.yunying.rispro.util.JsonResponseStatus;
import com.yunying.rispro.util.PageBean;
import com.yunying.rispro.vo.RisVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 检查信息表 前端控制器
 * </p>
 *
 * @author yunying
 * @since 2022-04-21
 */
@Controller
@RequestMapping("/examine")
public class ExamineController {

    @Autowired
    private IExamineService examineService;

    @Autowired
    private IPatientsService patientsService;

    @RequestMapping("/queryRisPager")
    @ResponseBody
    public JsonResponseBody<List<Map<String,Object>>> queryRisPager(RisVo risVo, PageBean pageBean){

        return examineService.queryRisPager(risVo,pageBean);
    }

    @RequestMapping("/addRisList")
    @ResponseBody
    public JsonResponseBody<?> addRisList(Examine examine){
        System.out.println(examine);
        if(examine.getExamineId()!=null && examine.getExamineId()!=0){
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("examine_id", examine.getExamineId());
            updateWrapper.set("examine_state", 1);
            boolean b = examineService.update(examine, updateWrapper);
            if(!b){
                throw new BusinessException(JsonResponseStatus.OPERATE_ERROR);
            }
        }else{
            examine.setExamineId(50+new Date().getTime());
            examine.setExamineState(1);
            boolean save = examineService.save(examine);
            if(!save){
                throw new BusinessException(JsonResponseStatus.OPERATE_ERROR);
            }
        }
        return new JsonResponseBody<>();
    }

    @RequestMapping("/updateRisList")
    @ResponseBody
    public JsonResponseBody<?> updateRisList(Patients patients){
        System.out.println(patients);
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("patients_id", patients.getPatientsId());
        boolean b = patientsService.update(patients,updateWrapper);
        if(!b){
            throw new BusinessException(JsonResponseStatus.OPERATE_ERROR);
        }
        return new JsonResponseBody<>();
    }

    @RequestMapping("/queryRisById")
    @ResponseBody
    private Map<String,Object> queryRisById(Long examineId){
        return examineService.queryRisById(examineId);
    }
}
